Imports System.Data.SqlClient
Imports System.ComponentModel

Public Class cls_cajaconcepto_

    Implements ICloneable

#Region "Variables ..."
    Private int_idcajaconcepto As Integer = 0
    Private str_nombre As String = ""
    Private int_tipocaja As cls_caja_.enu_tipo
    Private int_tipo As enu_tipo = 0
    Private int_tipoimporte As cls_cajaasiento_.enu_tipoimporte = 0
    Private int_idregistro As Integer = 0

    Private obj_registro As cls_registro_ = Nothing

    Private col_Items As Collections.Generic.List(Of cls_cajaconcepto_) = Nothing


    Public Enum enu_tipo
        <Description("Ninguno")> Ninguno = 0
        <Description("Solicita Proveedor")> Proveedor = 1
        <Description("Solicita Personal")> Personal = 2
        <Description("Solicita Cliente")> Cliente = 3
    End Enum

    Public Overrides Function ToString() As String
        Return nombre
    End Function
#End Region

#Region "Propiedades ..."
    ReadOnly Property This
        Get
            Return Me
        End Get
    End Property
    ReadOnly Property ThisDescripcion As String
        Get
            Return Me.ToString
        End Get
    End Property
    ReadOnly Property pKey
        Get
            Return idcajaconcepto
        End Get
    End Property
    Property idcajaconcepto() As Integer
        Get
            idcajaconcepto = int_idcajaconcepto
        End Get
        Set(ByVal value As Integer)
            int_idcajaconcepto = value
        End Set
    End Property
    Property nombre() As String
        Get
            nombre = str_nombre
        End Get
        Set(ByVal value As String)
            str_nombre = value
        End Set
    End Property
    Property tipocaja() As cls_caja_.enu_tipo
        Get
            tipocaja = int_tipocaja
        End Get
        Set(ByVal value As cls_caja_.enu_tipo)
            int_tipocaja = value
        End Set
    End Property
    Property tipo() As enu_tipo
        Get
            tipo = int_tipo
        End Get
        Set(ByVal value As enu_tipo)
            int_tipo = value
        End Set
    End Property
    Property tipoimporte As cls_cajaasiento_.enu_tipoimporte
        Get
            tipoimporte = int_tipoimporte
        End Get
        Set(value As cls_cajaasiento_.enu_tipoimporte)
            int_tipoimporte = value
        End Set
    End Property
    Property idregistro() As Integer
        Get
            idregistro = int_idregistro
        End Get
        Set(ByVal value As Integer)
            int_idregistro = value
        End Set
    End Property


    Property registro() As cls_registro_
        Get
            If IsNothing(obj_registro) Then
                obj_registro = New cls_registro_
                If Not obj_registro.Buscar(idregistro) Then
                    obj_registro = Nothing
                End If
            End If
            registro = obj_registro
        End Get
        Set(ByVal value As cls_registro_)
            obj_registro = value
        End Set
    End Property

    Property Items() As Collections.Generic.List(Of cls_cajaconcepto_)
        Get
            Items = col_Items
        End Get
        Set(ByVal value As Collections.Generic.List(Of cls_cajaconcepto_))
            col_Items = value
        End Set
    End Property
#End Region

    Private Sub CargarColeccion(ByVal obj_Reader As SqlDataReader)
        col_Items = New Collections.Generic.List(Of cls_cajaconcepto_)
        While obj_Reader.Read()
            Dim Elemento As New cls_cajaconcepto_
            Elemento.int_idcajaconcepto = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idcajaconcepto")))

            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("nombre"))) = False Then
                Elemento.str_nombre = (obj_Reader.GetValue(obj_Reader.GetOrdinal("nombre")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("tipocaja"))) = False Then
                Elemento.int_tipocaja = (obj_Reader.GetValue(obj_Reader.GetOrdinal("tipocaja")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("tipo"))) = False Then
                Elemento.int_tipo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("tipo")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idregistro"))) = False Then
                Elemento.int_idregistro = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idregistro")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("tipoimporte"))) = False Then
                Elemento.int_tipoimporte = (obj_Reader.GetValue(obj_Reader.GetOrdinal("tipoimporte")))
            End If
            col_Items.Add(Elemento)
        End While
    End Sub

    Private Sub CargarVariables(ByVal obj_Reader As SqlDataReader)
        obj_Reader.Read()
        int_idcajaconcepto = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idcajaconcepto")))

        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("nombre"))) = False Then
            str_nombre = (obj_Reader.GetValue(obj_Reader.GetOrdinal("nombre")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("tipocaja"))) = False Then
            int_tipocaja = (obj_Reader.GetValue(obj_Reader.GetOrdinal("tipocaja")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("tipo"))) = False Then
            int_tipo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("tipo")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idregistro"))) = False Then
            int_idregistro = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idregistro")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("tipoimporte"))) = False Then
            int_tipoimporte = (obj_Reader.GetValue(obj_Reader.GetOrdinal("tipoimporte")))
        End If
    End Sub

    Public Sub Nuevo(Optional ByVal ObtenerID As Boolean = False)
        Dim obj_Conexion As New cls_Conexion
        Try

            'Creacion de registro----------------------------------------------------
            If IsNothing(obj_registro) Then
                registro = New cls_registro_
                registro.fechacreado = Now
                registro.idusuariocreado = UsuarioLOG.idusuario
                registro.fechamodificado = Now
                registro.idusuariomodificado = UsuarioLOG.idusuario
                registro.idregistroestado = 1
                registro.idusuarioregistroestado = UsuarioLOG.idusuario
                registro.Nuevo(True)
                idregistro = obj_registro.idregistro
            End If
            '------------------------------------------------------------------------


            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "cajaconcepto__Nuevo"
                .Parameters.Add("@nombre", SqlDbType.NVarChar).Value = str_nombre
                .Parameters.Add("@tipocaja", SqlDbType.Int).Value = int_tipocaja
                .Parameters.Add("@tipo", SqlDbType.Int).Value = int_tipo
                .Parameters.Add("@idregistro", SqlDbType.Int).Value = int_idregistro
                .Parameters.Add("@tipoimporte", SqlDbType.Int).Value = int_tipoimporte
                .ExecuteNonQuery()
            End With
            If ObtenerID Then
                CargarUltimo()
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Crear el registro de cajaconcepto_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Sub Modificar()
        Dim obj_Conexion As New cls_Conexion
        Try

            'Modificacion de registro------------------------------------------------
            registro.fechamodificado = Now : registro.idusuariomodificado = UsuarioLOG.idusuario
            registro.Modificar()
            '------------------------------------------------------------------------

            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "cajaconcepto__Modificar"
                .Parameters.Add("@idcajaconcepto", SqlDbType.Int).Value = int_idcajaconcepto
                .Parameters.Add("@nombre", SqlDbType.NVarChar).Value = str_nombre
                .Parameters.Add("@tipocaja", SqlDbType.Int).Value = int_tipocaja
                .Parameters.Add("@tipo", SqlDbType.Int).Value = int_tipo
                .Parameters.Add("@idregistro", SqlDbType.Int).Value = int_idregistro
                .Parameters.Add("@tipoimporte", SqlDbType.Int).Value = int_tipoimporte
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Modificar el registro de cajaconcepto_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Sub Eliminar(ByVal idcajaconcepto As Integer)
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "cajaconcepto__Eliminar"
                .Parameters.Add("@idcajaconcepto", SqlDbType.Int).Value = idcajaconcepto
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Eliminar el registro de cajaconcepto_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Function Buscar(ByVal idcajaconcepto As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            Buscar = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "cajaconcepto__Buscar"
                .Parameters.Add("@idcajaconcepto", SqlDbType.Int).Value = idcajaconcepto
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
                Buscar = True
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Buscar el registro de cajaconcepto_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Sub CargarUltimo()
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "cajaconcepto__CargarUltimo"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al CargarUltimo registro de cajaconcepto_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Sub

    Public Function CargarTodos(Optional ByVal idregistroestado As Integer = 1) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarTodos = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "cajaconcepto__CargarTodos"
                .Parameters.Add("@idregistroestado", SqlDbType.Int).Value = idregistroestado
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarTodos = True
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al CargarTodos los registros de cajaconcepto_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function CargarXtipocaja(ByVal prm_tipocaja As cls_caja_.enu_tipo) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarXtipocaja = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "cajaconcepto__CargarXtipocaja"
                .Parameters.Add("@tipocaja", SqlDbType.Int).Value = prm_tipocaja
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarXtipocaja = True
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al CargarXtipocaja los registros de cajaconcepto_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function Clone() As Object Implements System.ICloneable.Clone
        Dim obj_cajaconcepto_Clon As New cls_cajaconcepto_
        obj_cajaconcepto_Clon.int_idcajaconcepto = Me.int_idcajaconcepto
        obj_cajaconcepto_Clon.str_nombre = Me.str_nombre
        obj_cajaconcepto_Clon.int_tipocaja = Me.int_tipocaja
        obj_cajaconcepto_Clon.int_tipo = Me.int_tipo
        obj_cajaconcepto_Clon.int_idregistro = Me.int_idregistro
        obj_cajaconcepto_Clon.int_tipoimporte = Me.int_tipoimporte
        obj_cajaconcepto_Clon.col_Items = Me.col_Items
        Return obj_cajaconcepto_Clon
    End Function

End Class
